package com.usian.behavior.rabbitmq;


import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.behavior.dtos.FollowBehaviorDto;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
public class BehaviorMQ {

    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;

    //监听方法  routingkey
    @RabbitListener(queues = {"BehaviorQueue"})
    @RabbitHandler
    public void Behavior(@Payload String msg, Message message, Channel channel) throws IOException {//传msg接收消息
        channel.basicAck(message.getMessageProperties().getContentLength(),true);
        JSONObject jsonObject = JSONObject.parseObject(msg);
        FollowBehaviorDto dto = JSONObject.toJavaObject(jsonObject, FollowBehaviorDto.class);
        apFollowBehaviorService.saveFollowBehavior(dto);
    }

}
